import stackless
from event import Event

import pos as _pos

class Actor:
    def __init__(self):

        self.channel=stackless.channel()
        self.tasklet=stackless.tasklet(self.defaultEventHandler)()

    def defaultEventHandler(self):
        while True :
            event=self.channel.receive()

            if event=='quit' :
                break

            print event
            
            stackless.schedule()

class Extended:
    
    def __init__(self,world_,pos,size=(10,10),**kwargs):

        if not isinstance(pos,_pos.Position):
            raise TypeError('arg pos must be a Position instance')

        self.pos=pos
        self.size=size

        self.world=world_

        channel=getattr(self,'channel',None)

        stackless.tasklet(self.world.channel.send)(
            Event(subject='join',pos=self.pos,obj=self,**kwargs)
            )

        if hasattr(self,'action'):
            stackless.tasklet(self.action)()

    def update(self):
        stackless.tasklet(self.world.channel.send)(Event(subject='move',pos=self.pos,obj=self))

    def remove(self):

        if hasattr(self,'channel'):
            self.channel.send(Event(subject='quit'))

        stackless.tasklet(self.world.channel.send)(Event(subject='remove',obj=self,pos=self.pos))

